//package edu.ncsu.csc.camera;
// for SPOT
//import com.sun.squawk.util.StringTokenizer;
// for PC
import java.util.StringTokenizer;
import java.awt.image.BufferedImage;
import javax.imageio.*;
import java.io.*;

public class FPacket
{
	int[][] red;
	int[][] green;
	int[][] blue;
	int width;
	int height;
	
	public FPacket(int[][] red, int[][] green, int[][] blue, int width, int height)
	{
		this.red = red;
		this.green = green;
		this.blue = blue;
		this.width = width;
		this.height = height;
	}
	
	public static FPacket build(ByteVector bv)
	{
		int _width = 0xFF & bv.get(1);
		int _height = 0xFF & bv.get(2);

		int[][] _red = new int[_width][_height];
		int[][] _green = new int[_width][_height];
		int[][] _blue = new int[_width][_height];
		
		for(int h = 0; h < _height; h++)
		{
			for(int w = 0; w < _width; w++)
			{
				_red[w][h] = 0xFF & bv.get(4 + h * (_width * 3 + 1) + 0 + w * 3);
				_green[w][h] = 0xFF & bv.get(4 + h * (_width * 3 + 1) + 1 + w * 3);
				_blue[w][h] = 0xFF & bv.get(4 + h * (_width * 3 + 1) + 2 + w * 3);
			}
		}
		return new FPacket(_red, _green, _blue, _width, _height);
	}

	public String toString()
	{
		return null;
	}


	public void toFile(String name) throws IOException
	{
		BufferedImage image = new BufferedImage(width * 2, height, BufferedImage.TYPE_INT_RGB);
		for(int h = 0; h < height; h++)
		{
			for(int w = 0; w < width * 2; w++)
			{
				// this expects an int for red, green and blue
				// in java, byte order is Most Significant Byte first
				image.setRGB(w, h, (red[w/2][h] << 16) +(green[w/2][h] << 8) + (blue[w/2][h]));
			}
		}
		File output = new File(name + ".png");  
		ImageIO.write(image, "png", output); 
	}
 
}